package com.wyf.android.advance.activityRecycleTest

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.wyf.android.advance.BaseActivity
import com.wyf.android.advance.R
import com.wyf.android.baselib.util.LogUtil

/**
 * Activity销毁重建Intent中的数据、有id的View的状态等数据不需要开发者进行保存和恢复，系统会自动完成
 * 其它的由开发者自定义的变量等需要数据需要开发者主动进行保存和恢复
 */
class RecycleTestActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycle_test)

        initIntentData(intent.extras)
        initSavedInstanceState(savedInstanceState)
    }

    private fun initSavedInstanceState(extras: Bundle?) {
//        extras?.run {
//            val params1 = if (containsKey(KEY_PARAMS_1)) {
//                getString(KEY_PARAMS_1, "def")
//            } else ""
//            val params2 = if (containsKey(KEY_PARAMS_2)) {
//                getInt(KEY_PARAMS_2, 0)
//            } else -1
//            LogUtil.d("Intent参数值：params1=${params1},params2=${params2}")
//        }
    }

    private fun initIntentData(extras: Bundle?) {
        extras?.run {
            val params1 = if (containsKey(KEY_PARAMS_1)) {
                getString(KEY_PARAMS_1, "def")
            } else ""
            val params2 = if (containsKey(KEY_PARAMS_2)) {
                getInt(KEY_PARAMS_2, 0)
            } else -1
            LogUtil.d("Intent参数值：params1=${params1},params2=${params2}")
        } ?: LogUtil.d("Intent参数为null")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        LogUtil.d("onSaveInstanceState")
    }

    companion object {
        private const val KEY_PARAMS_1 = "key_params_1"
        private const val KEY_PARAMS_2 = "key_params_2"
        fun start(context: Context, params1: String, params2: Int) {
            context.startActivity(buildIntent(context, params1, params2))
        }

        private fun buildIntent(context: Context, params1: String, params2: Int): Intent {
            Activity::class.java
            return Intent(context, RecycleTestActivity::class.java).apply {
                putExtra(KEY_PARAMS_1, params1)
                putExtra(KEY_PARAMS_2, params2)
            }
        }
    }
}